home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / python2.6 / email / charset.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2009-11-11  |  13KB  |  359 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'Charset',
  6.     'add_alias',
  7.     'add_charset',
  8.     'add_codec']
  9. import email.base64mime as email
  10. import email.quoprimime as email
  11. from email import errors
  12. from email.encoders import encode_7or8bit
  13. QP = 1
  14. BASE64 = 2
  15. SHORTEST = 3
  16. MISC_LEN = 7
  17. DEFAULT_CHARSET = 'us-ascii'
  18. CHARSETS = {
  19.     'iso-8859-1': (QP, QP, None),
  20.     'iso-8859-2': (QP, QP, None),
  21.     'iso-8859-3': (QP, QP, None),
  22.     'iso-8859-4': (QP, QP, None),
  23.     'iso-8859-9': (QP, QP, None),
  24.     'iso-8859-10': (QP, QP, None),
  25.     'iso-8859-13': (QP, QP, None),
  26.     'iso-8859-14': (QP, QP, None),
  27.     'iso-8859-15': (QP, QP, None),
  28.     'iso-8859-16': (QP, QP, None),
  29.     'windows-1252': (QP, QP, None),
  30.     'viscii': (QP, QP, None),
  31.     'us-ascii': (None, None, None),
  32.     'big5': (BASE64, BASE64, None),
  33.     'gb2312': (BASE64, BASE64, None),
  34.     'euc-jp': (BASE64, None, 'iso-2022-jp'),
  35.     'shift_jis': (BASE64, None, 'iso-2022-jp'),
  36.     'iso-2022-jp': (BASE64, None, None),
  37.     'koi8-r': (BASE64, BASE64, None),
  38.     'utf-8': (SHORTEST, BASE64, 'utf-8'),
  39.     '8bit': (None, BASE64, 'utf-8') }
  40. ALIASES = {
  41.     'latin_1': 'iso-8859-1',
  42.     'latin-1': 'iso-8859-1',
  43.     'latin_2': 'iso-8859-2',
  44.     'latin-2': 'iso-8859-2',
  45.     'latin_3': 'iso-8859-3',
  46.     'latin-3': 'iso-8859-3',
  47.     'latin_4': 'iso-8859-4',
  48.     'latin-4': 'iso-8859-4',
  49.     'latin_5': 'iso-8859-9',
  50.     'latin-5': 'iso-8859-9',
  51.     'latin_6': 'iso-8859-10',
  52.     'latin-6': 'iso-8859-10',
  53.     'latin_7': 'iso-8859-13',
  54.     'latin-7': 'iso-8859-13',
  55.     'latin_8': 'iso-8859-14',
  56.     'latin-8': 'iso-8859-14',
  57.     'latin_9': 'iso-8859-15',
  58.     'latin-9': 'iso-8859-15',
  59.     'latin_10': 'iso-8859-16',
  60.     'latin-10': 'iso-8859-16',
  61.     'cp949': 'ks_c_5601-1987',
  62.     'euc_jp': 'euc-jp',
  63.     'euc_kr': 'euc-kr',
  64.     'ascii': 'us-ascii' }
  65. CODEC_MAP = {
  66.     'gb2312': 'eucgb2312_cn',
  67.     'big5': 'big5_tw',
  68.     'us-ascii': None }
  69.  
  70. def add_charset(charset, header_enc = None, body_enc = None, output_charset = None):
  71.     """Add character set properties to the global registry.
  72.  
  73.     charset is the input character set, and must be the canonical name of a
  74.     character set.
  75.  
  76.     Optional header_enc and body_enc is either Charset.QP for
  77.     quoted-printable, Charset.BASE64 for base64 encoding, Charset.SHORTEST for
  78.     the shortest of qp or base64 encoding, or None for no encoding.  SHORTEST
  79.     is only valid for header_enc.  It describes how message headers and
  80.     message bodies in the input charset are to be encoded.  Default is no
  81.     encoding.
  82.  
  83.     Optional output_charset is the character set that the output should be
  84.     in.  Conversions will proceed from input charset, to Unicode, to the
  85.     output charset when the method Charset.convert() is called.  The default
  86.     is to output in the same character set as the input.
  87.  
  88.     Both input_charset and output_charset must have Unicode codec entries in
  89.     the module's charset-to-codec mapping; use add_codec(charset, codecname)
  90.     to add codecs the module does not know about.  See the codecs module's
  91.     documentation for more information.
  92.     """
  93.     if body_enc == SHORTEST:
  94.         raise ValueError('SHORTEST not allowed for body_enc')
  95.     body_enc == SHORTEST
  96.     CHARSETS[charset] = (header_enc, body_enc, output_charset)
  97.  
  98.  
  99. def add_alias(alias, canonical):
  100.     """Add a character set alias.
  101.  
  102.     alias is the alias name, e.g. latin-1
  103.     canonical is the character set's canonical name, e.g. iso-8859-1
  104.     """
  105.     ALIASES[alias] = canonical
  106.  
  107.  
  108. def add_codec(charset, codecname):
  109.     '''Add a codec that map characters in the given charset to/from Unicode.
  110.  
  111.     charset is the canonical name of a character set.  codecname is the name
  112.     of a Python codec, as appropriate for the second argument to the unicode()
  113.     built-in, or to the encode() method of a Unicode string.
  114.     '''
  115.     CODEC_MAP[charset] = codecname
  116.  
  117.  
  118. class Charset:
  119.     """Map character sets to their email properties.
  120.  
  121.     This class provides information about the requirements imposed on email
  122.     for a specific character set.  It also provides convenience routines for
  123.     converting between character sets, given the availability of the
  124.     applicable codecs.  Given a character set, it will do its best to provide
  125.     information on how to use that character set in an email in an
  126.     RFC-compliant way.
  127.  
  128.     Certain character sets must be encoded with quoted-printable or base64
  129.     when used in email headers or bodies.  Certain character sets must be
  130.     converted outright, and are not allowed in email.  Instances of this
  131.     module expose the following information about a character set:
  132.  
  133.     input_charset: The initial character set specified.  Common aliases
  134.                    are converted to their `official' email names (e.g. latin_1
  135.                    is converted to iso-8859-1).  Defaults to 7-bit us-ascii.
  136.  
  137.     header_encoding: If the character set must be encoded before it can be
  138.                      used in an email header, this attribute will be set to
  139.                      Charset.QP (for quoted-printable), Charset.BASE64 (for
  140.                      base64 encoding), or Charset.SHORTEST for the shortest of
  141.                      QP or BASE64 encoding.  Otherwise, it will be None.
  142.  
  143.     body_encoding: Same as header_encoding, but describes the encoding for the
  144.                    mail message's body, which indeed may be different than the
  145.                    header encoding.  Charset.SHORTEST is not allowed for
  146.                    body_encoding.
  147.  
  148.     output_charset: Some character sets must be converted before the can be
  149.                     used in email headers or bodies.  If the input_charset is
  150.                     one of them, this attribute will contain the name of the
  151.                     charset output will be converted to.  Otherwise, it will
  152.                     be None.
  153.  
  154.     input_codec: The name of the Python codec used to convert the
  155.                  input_charset to Unicode.  If no conversion codec is
  156.                  necessary, this attribute will be None.
  157.  
  158.     output_codec: The name of the Python codec used to convert Unicode
  159.                   to the output_charset.  If no conversion codec is necessary,
  160.                   this attribute will have the same value as the input_codec.
  161.     """
  162.     
  163.     def __init__(self, input_charset = DEFAULT_CHARSET):
  164.         
  165.         try:
  166.             if isinstance(input_charset, unicode):
  167.                 input_charset.encode('ascii')
  168.             else:
  169.                 input_charset = unicode(input_charset, 'ascii')
  170.         except UnicodeError:
  171.             raise errors.CharsetError(input_charset)
  172.  
  173.         input_charset = input_charset.lower()
  174.         self.input_charset = ALIASES.get(input_charset, input_charset)
  175.         (henc, benc, conv) = CHARSETS.get(self.input_charset, (SHORTEST, BASE64, None))
  176.         if not conv:
  177.             conv = self.input_charset
  178.         
  179.         self.header_encoding = henc
  180.         self.body_encoding = benc
  181.         self.output_charset = ALIASES.get(conv, conv)
  182.         self.input_codec = CODEC_MAP.get(self.input_charset, self.input_charset)
  183.         self.output_codec = CODEC_MAP.get(self.output_charset, self.output_charset)
  184.  
  185.     
  186.     def __str__(self):
  187.         return self.input_charset.lower()
  188.  
  189.     __repr__ = __str__
  190.     
  191.     def __eq__(self, other):
  192.         return str(self) == str(other).lower()
  193.  
  194.     
  195.     def __ne__(self, other):
  196.         return not self.__eq__(other)
  197.  
  198.     
  199.     def get_body_encoding(self):
  200.         '''Return the content-transfer-encoding used for body encoding.
  201.  
  202.         This is either the string `quoted-printable\' or `base64\' depending on
  203.         the encoding used, or it is a function in which case you should call
  204.         the function with a single argument, the Message object being
  205.         encoded.  The function should then set the Content-Transfer-Encoding
  206.         header itself to whatever is appropriate.
  207.  
  208.         Returns "quoted-printable" if self.body_encoding is QP.
  209.         Returns "base64" if self.body_encoding is BASE64.
  210.         Returns "7bit" otherwise.
  211.         '''
  212.         if not self.body_encoding != SHORTEST:
  213.             raise AssertionError
  214.         if self.body_encoding == QP:
  215.             return 'quoted-printable'
  216.         if self.body_encoding == BASE64:
  217.             return 'base64'
  218.         return encode_7or8bit
  219.  
  220.     
  221.     def convert(self, s):
  222.         '''Convert a string from the input_codec to the output_codec.'''
  223.         if self.input_codec != self.output_codec:
  224.             return unicode(s, self.input_codec).encode(self.output_codec)
  225.         return s
  226.  
  227.     
  228.     def to_splittable(self, s):
  229.         """Convert a possibly multibyte string to a safely splittable format.
  230.  
  231.         Uses the input_codec to try and convert the string to Unicode, so it
  232.         can be safely split on character boundaries (even for multibyte
  233.         characters).
  234.  
  235.         Returns the string as-is if it isn't known how to convert it to
  236.         Unicode with the input_charset.
  237.  
  238.         Characters that could not be converted to Unicode will be replaced
  239.         with the Unicode replacement character U+FFFD.
  240.         """
  241.         if isinstance(s, unicode) or self.input_codec is None:
  242.             return s
  243.         
  244.         try:
  245.             return unicode(s, self.input_codec, 'replace')
  246.         except LookupError:
  247.             self.input_codec is None
  248.             self.input_codec is None
  249.             return s
  250.  
  251.  
  252.     
  253.     def from_splittable(self, ustr, to_output = True):
  254.         """Convert a splittable string back into an encoded string.
  255.  
  256.         Uses the proper codec to try and convert the string from Unicode back
  257.         into an encoded format.  Return the string as-is if it is not Unicode,
  258.         or if it could not be converted from Unicode.
  259.  
  260.         Characters that could not be converted from Unicode will be replaced
  261.         with an appropriate character (usually '?').
  262.  
  263.         If to_output is True (the default), uses output_codec to convert to an
  264.         encoded format.  If to_output is False, uses input_codec.
  265.         """
  266.         if to_output:
  267.             codec = self.output_codec
  268.         else:
  269.             codec = self.input_codec
  270.         if not isinstance(ustr, unicode) or codec is None:
  271.             return ustr
  272.         
  273.         try:
  274.             return ustr.encode(codec, 'replace')
  275.         except LookupError:
  276.             codec is None
  277.             codec is None
  278.             return ustr
  279.  
  280.  
  281.     
  282.     def get_output_charset(self):
  283.         '''Return the output character set.
  284.  
  285.         This is self.output_charset if that is not None, otherwise it is
  286.         self.input_charset.
  287.         '''
  288.         if not self.output_charset:
  289.             pass
  290.         return self.input_charset
  291.  
  292.     
  293.     def encoded_header_len(self, s):
  294.         '''Return the length of the encoded header string.'''
  295.         cset = self.get_output_charset()
  296.         if self.header_encoding == BASE64:
  297.             return email.base64mime.base64_len(s) + len(cset) + MISC_LEN
  298.         if self.header_encoding == QP:
  299.             return email.quoprimime.header_quopri_len(s) + len(cset) + MISC_LEN
  300.         if self.header_encoding == SHORTEST:
  301.             lenb64 = email.base64mime.base64_len(s)
  302.             lenqp = email.quoprimime.header_quopri_len(s)
  303.             return min(lenb64, lenqp) + len(cset) + MISC_LEN
  304.         return len(s)
  305.  
  306.     
  307.     def header_encode(self, s, convert = False):
  308.         '''Header-encode a string, optionally converting it to output_charset.
  309.  
  310.         If convert is True, the string will be converted from the input
  311.         charset to the output charset automatically.  This is not useful for
  312.         multibyte character sets, which have line length issues (multibyte
  313.         characters must be split on a character, not a byte boundary); use the
  314.         high-level Header class to deal with these issues.  convert defaults
  315.         to False.
  316.  
  317.         The type of encoding (base64 or quoted-printable) will be based on
  318.         self.header_encoding.
  319.         '''
  320.         cset = self.get_output_charset()
  321.         if convert:
  322.             s = self.convert(s)
  323.         
  324.         if self.header_encoding == BASE64:
  325.             return email.base64mime.header_encode(s, cset)
  326.         if self.header_encoding == QP:
  327.             return email.quoprimime.header_encode(s, cset, maxlinelen = None)
  328.         if self.header_encoding == SHORTEST:
  329.             lenb64 = email.base64mime.base64_len(s)
  330.             lenqp = email.quoprimime.header_quopri_len(s)
  331.             if lenb64 < lenqp:
  332.                 return email.base64mime.header_encode(s, cset)
  333.             return email.quoprimime.header_encode(s, cset, maxlinelen = None)
  334.         self.header_encoding == SHORTEST
  335.         return s
  336.  
  337.     
  338.     def body_encode(self, s, convert = True):
  339.         '''Body-encode a string and convert it to output_charset.
  340.  
  341.         If convert is True (the default), the string will be converted from
  342.         the input charset to output charset automatically.  Unlike
  343.         header_encode(), there are no issues with byte boundaries and
  344.         multibyte charsets in email bodies, so this is usually pretty safe.
  345.  
  346.         The type of encoding (base64 or quoted-printable) will be based on
  347.         self.body_encoding.
  348.         '''
  349.         if convert:
  350.             s = self.convert(s)
  351.         
  352.         if self.body_encoding is BASE64:
  353.             return email.base64mime.body_encode(s)
  354.         if self.body_encoding is QP:
  355.             return email.quoprimime.body_encode(s)
  356.         return s
  357.  
  358.  
  359.